/*
 * @Author: maskMan
 * @LastEditTime: 2024-11-19 09:00:04
 * @Descripttion:网络请求管理
 */
enum HeadType {
  JSON = 'application/json',
  URLS = 'application/x-www-form-urlencoded',
  FORM = 'multipart/form-data'
}
import { AxiosInstance, AxiosStatic, RawAxiosRequestHeaders } from './axios';
import Ax from './axios';
import Config from '@/utils/config';
import { getToken } from '@/utils/cookies';
const axios = Ax as unknown as AxiosStatic;
export default class Request {
  public static baseURL = Config.baseUrl;
  private static _resInstance: AxiosInstance;
  private static _defaultHead: RawAxiosRequestHeaders = {
    'Content-Type': HeadType.JSON
  };
  /**
   * @description: 创建请求实例
   * @return {*}
   */
  private static _createAxios() {
    try {
      this._resInstance = axios.create({
        baseURL: this.baseURL,
        headers: this._defaultHead,
        timeout: 10000
      });
      // 响应返回参数格式
      this._resInstance.interceptors.response.use(
        (res: any) => {
          return res?.data || null;
        },
        (error) => {
          if (error?.response) {
            console.error(error.message);
            return null;
          }
          console.error(error.message);
          return null;
        }
      );
      // 发送拦截
      this._resInstance.interceptors.request.use(
        (req: any) => {
          const token = getToken();
          if (token) {
            req.headers.setAuthorization(`Bearer ${token}`);
          }
          return req;
        },
        (error) => {
          if (error.response) {
            console.error(error.message);
            return null;
          }
          console.error(error.message);
          return null;
        }
      );
    } catch (error) {
      console.error('请求返回错误数据', error);
    }
  }
  /**
   * @description: get 获取请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static get<T>(url: string, params: Obj = null, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<T> {
    return new Promise((resolve) => {
      this._resInstance == undefined && this._createAxios();
      return this._resInstance
        .get(url, { params: params, baseURL: this.baseURL, headers })
        .then((res) => {
          return resolve(res as unknown as T);
        })
        .catch((error: string) => {
          console.log('get请求错误', error);
          return null;
        });
    });
  }
  /**
   * @description: post 新增请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static post<T>(url: string, data: Obj = null, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<T> {
    return new Promise((resolve) => {
      this._resInstance == undefined && this._createAxios();
      return this._resInstance
        .post(url, data, { baseURL: this.baseURL, headers })
        .then((res) => {
          return resolve(res as unknown as T);
        })
        .catch((error: string) => {
          console.log('post请求错误', error);
          return resolve(null);
        });
    });
  }
  /**
   * @description: delete 删除请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static delete<T>(url: string, params: Obj = null, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<T> {
    return new Promise((resolve) => {
      this._resInstance == undefined && this._createAxios();
      return this._resInstance
        .delete(url, { params: params, baseURL: this.baseURL, headers })
        .then((res) => {
          return resolve(res as unknown as T);
        })
        .catch((error: string) => {
          console.log('delete请求错误', error);
          return resolve(null);
        });
    });
  }
  /**
   * @description: put 替换请求
   * @param {string} url 请求路由
   * @param {data} data 请求参数数据
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static put<T>(url: string, data: Obj = null, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<T> {
    return new Promise((resolve) => {
      this._resInstance == undefined && this._createAxios();
      return this._resInstance
        .put(url, data, { baseURL: this.baseURL, headers })
        .then((res) => {
          return resolve(res as unknown as T);
        })
        .catch((error: string) => {
          console.log('put请求错误', error);
          return resolve(null);
        });
    });
  }
  /**
   * @description: patch 更新请求
   * @param {string} url 请求路由
   * @param {data} data 请求参数数据
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static patch<T>(url: string, data: Obj = null, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<T> {
    this._resInstance == undefined && this._createAxios();
    return new Promise((resolve) => {
      this._resInstance
        .patch(url, data, { baseURL: this.baseURL, headers })
        .then((res) => {
          return resolve(res as unknown as T);
        })
        .catch((error: string) => {
          console.log('patch请求错误', error);
          return resolve(null);
        });
    });
  }
}
